package apigateway.serviceRegister;

import org.I0Itec.zkclient.ZkClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * Created by 小泽玛丽罗 on 2017/3/1.
 */
@Component
public class registeService {
    private Logger logger= LoggerFactory.getLogger(registeService.class);
    @Value("${zookeeper.ip}")
    private String zookeeperIp;
    @Value("${service.ip}")
    private String serviceIp;
    @Value("${service.port}")
    private int servicPort;
    @Value("${service.type}")
    private String serviceType;
    @Value("${service.registePath}")
    private String registePath;
    @Value("${service.parentPath}")
    private String parentPath;
    @Value("${service.location}")
    private String location;
    @Value("${service.name}")
    private String serviceName;
    @Value("${service.category}")
    private String serviceCategory;
    @Value("${service.weight}")
    private int weight;
    @PostConstruct
    public void registeService(){
        try {
            serviceIp= InetAddress.getLocalHost().getHostAddress();
            //TODO 暂时zk的ip地址，之后都从
            zookeeperIp=serviceIp+":2181";
            logger.info("动态获得服务当前的ip地址"+serviceIp);
            ServiceNode serviceNode=new ServiceNode(serviceIp,servicPort,
                    serviceType,registePath,location,serviceName,serviceCategory,weight);
            ZkClient zkClient=new ZkClient(zookeeperIp,
                    5000);
            if(zkClient.exists(registePath)){
                zkClient.writeData(registePath, serviceNode);
            }else{
                zkClient.createPersistent(parentPath,true);
                zkClient.createEphemeral(registePath,serviceNode);
                //TODO 设置ZNode 的数据版本，version
                //TODO 问题是在服务节点与zk断开连接后，然后再连接上，同时触发了childchanged和datachanged 两个事件
            }
            logger.info("register success service  Path:"+serviceNode.toString()+" zookeeper ："+zookeeperIp);
        } catch (RuntimeException e) {
            e.printStackTrace();
            logger.error(e.getMessage(),e);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        /*ZkClient zkClient=new ZkClient("192.168.91.128:2181",5000);
        ServiceNode zooKeeperNode=new ServiceNode();
        zooKeeperNode.setIp("10.22.75.158");
        zooKeeperNode.setPort(8888);
        long start=System.currentTimeMillis();
        zkClient.createEphemeral("/temp",zooKeeperNode);
        System.out.println(System.currentTimeMillis()-start);
        long mid=System.currentTimeMillis();
        ServiceNode readNode=zkClient.readData("/temp");
        System.out.println(System.currentTimeMillis()-mid);
        System.out.println(readNode.getIp()+":"+readNode.getPort());*/
    }


}
